package io.renren.modules.appManagement.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.appManagement.entity.AppUpdateEntity;
import io.renren.modules.appManagement.service.AppUpdateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author wxy
 * @email sharonwang_xy@163.com
 * @date 2018-10-31 16:29:31
 */
@RestController
@RequestMapping("appManagement/appupdate")
public class AppUpdateController {
    @Autowired
    private AppUpdateService appUpdateService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("appManagement:appupdate:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<AppUpdateEntity> appUpdateList = appUpdateService.queryList(query);
        int total = appUpdateService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(appUpdateList, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("appManagement:appupdate:info")
    public R info(@PathVariable("id") Integer id) {
        AppUpdateEntity appUpdate = appUpdateService.queryObject(id);

        return R.ok().put("appUpdate", appUpdate);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("appManagement:appupdate:save")
    public R save(@RequestBody AppUpdateEntity appUpdate) {

        int indexno = appUpdate.getMainno() * 1000 * 1000 + appUpdate.getMinorno() * 1000 + appUpdate.getReviseno();
        appUpdate.setIndexno(indexno);

        // 查询是否已存在该版本
        Map<String, Object> map = new HashMap<>();
        map.put("appid", appUpdate.getAppid());
        map.put("mainno", appUpdate.getMainno());
        map.put("minorno", appUpdate.getMinorno());
        map.put("reviseno", appUpdate.getReviseno());
        List<AppUpdateEntity> list = appUpdateService.queryListByParam(map);
        if (list.size() > 0) {
            return R.error(401, "该版本已存在，请重新输入版本号，或编辑已存在的版本信息！");
        }
        appUpdateService.save(appUpdate);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("appManagement:appupdate:update")
    public R update(@RequestBody AppUpdateEntity appUpdate) {

        int indexno = appUpdate.getMainno() * 1000 * 1000 + appUpdate.getMinorno() * 1000 + appUpdate.getReviseno();
        appUpdate.setIndexno(indexno);

        // 查询是否已存在该版本
        Map<String, Object> map = new HashMap<>();
        map.put("appid", appUpdate.getAppid());
        map.put("mainno", appUpdate.getMainno());
        map.put("minorno", appUpdate.getMinorno());
        map.put("reviseno", appUpdate.getReviseno());
        List<AppUpdateEntity> list = appUpdateService.queryListByParam(map);
        // 修改为新的版本号
        if (list.size() < 1) {
            appUpdateService.update(appUpdate);
            return R.ok();
        }
        // 已存在重复的版本号
        if (list.size() > 1) {
            return R.error(401, "该版本已存在，请重新输入版本号，或编辑已存在的版本信息！");
        }
        // 存在一个，且这一个就是自身
        if (list.get(0).getId().equals(appUpdate.getId())) {
            appUpdateService.update(appUpdate);
            return R.ok();
        }
        return R.error(401, "该版本已存在，请重新输入版本号，或编辑已存在的版本信息！");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("appManagement:appupdate:delete")
    public R delete(@RequestBody Integer[] ids) {
        appUpdateService.deleteBatch(ids);

        return R.ok();
    }

}
